package com.bawei.module_home.viewmodel

import androidx.lifecycle.viewModelScope
import com.bawei.library_mvi.model.BaseViewModel
import com.bawei.library_net.entity.ResponseEntity
import com.bawei.module_home.intent.SimpleTypeIntent
import com.bawei.module_home.intent.SimpleVideoIntent
import com.bawei.module_home.model.entity.SimpleTypeEntity
import com.bawei.module_home.model.service.VideoService
import com.bawei.module_home.model.service.VideoTypeService
import com.bawei.module_home.state.SimpleTypeState
import com.bawei.module_home.state.SimpleVideoState
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch

/**
 * @Author : yaotianxue
 * @Time : On 2023/6/25 09:21
 * @Description : SimpleVideoViewModel
 */
class SimpleVideoViewModel:BaseViewModel<SimpleVideoIntent>() {
    //网络请求
    val service:VideoService by lazy{VideoService()}

    //UIState
    private val _states = MutableStateFlow<SimpleVideoState>(SimpleVideoState.Init)//初始化默认的状态
    val state:StateFlow<SimpleVideoState>
        get() = _states

    //1:处理用户的意图
    override fun handleIntent() {
        viewModelScope.launch {
            intents.consumeAsFlow().collect{
                when(it){
                    //2：判断意图类型-》发起网络请求
                    is SimpleVideoIntent.RecommendSimpleVideo -> {
                        doRecommendSimpleVideo(it)
                    }
                    is SimpleVideoIntent.SimpleVideoByChannelId -> {
                        doSimpleVideoByChannelId(it)
                    }
                }
            }
        }
    }

    private fun doSimpleVideoByChannelId(it: SimpleVideoIntent.SimpleVideoByChannelId) {
        viewModelScope.launch{
            flow {
                val recommendSimpleVideo = service.getSimpleVideoByChannelId(it.channelId,it.page, it.pagesize)
                emit(recommendSimpleVideo)
            }.catch { execption->
                _states.value = SimpleVideoState.Error(execption)
            }.collect{
                if(it.data === null || it.data.equals("")|| it.data.size ===0){
                    _states.value = SimpleVideoState.Empty
                }else{
                    _states.value = SimpleVideoState.SimpleVideoByChannelId(it.data)
                }
            }
        }
    }

    //3：网络请求数据->更新state状态
    private fun doRecommendSimpleVideo(it: SimpleVideoIntent.RecommendSimpleVideo) {
        viewModelScope.launch{
            flow {
                val recommendSimpleVideo = service.getRecommendSimpleVideo(it.page, it.pagesize)
                emit(recommendSimpleVideo)
            }.catch { execption->
                _states.value = SimpleVideoState.Error(execption)
            }.collect{
                if(it.data === null || it.data.equals("")|| it.data.size ===0){
                    _states.value = SimpleVideoState.Empty
                }else{
                    _states.value = SimpleVideoState.RecommendSimpleVideo(it.data)
                }
            }
        }
    }


}